package com.arina.onepagesmarthome;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.view.View;
import android.widget.ToggleButton;


public class Relay{
	public MasterBoard System;
	int SubnetID;
	int DeviceID;
	int NumberChannel;
	public ToggleButton[] ArrButton;
	public boolean[] Status;



	public Relay(MasterBoard system,int mSubnetID, int mDeviceID,int nChannel) {
		super();
		System = system;
		SubnetID=mSubnetID;
		DeviceID = mDeviceID;
		NumberChannel=nChannel;
		ArrButton=new ToggleButton[nChannel];
		Status=new boolean[nChannel];
	}
	
	public void setOnClickListener(final COMMAND_TYPE cmdtype, final int brightnesslevel)
	{
		for(int i=0;i<ArrButton.length;i++)
		{
			final int index=i;
			if(ArrButton[i]!=null){
				ArrButton[i].setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						if(cmdtype==COMMAND_TYPE.REVERSE)
						{
							Thread th=new Thread(){
								public void run(){
									Reverse(index+1);
								};
							};
							th.start();
						}

						if(cmdtype==COMMAND_TYPE.ON_OFF)
						{
							if(brightnesslevel==100)
							{
								Thread th=new Thread(){
									public void run(){
										On(index+1);
									};
								};
								th.start();
							}

							if(brightnesslevel==0)
							{
								Thread th=new Thread(){
									public void run(){
										Off(index+1);
									};
								};
								th.start();
							}

						}
					}
				});
			}
		}
	}

	public void On(int channel)
	{
		ElectrictCommand controlSH=new ElectrictCommand(System, COMMAND_TYPE.ON_OFF, SubnetID, DeviceID, channel, 100, 0);
		try {
			controlSH.sendCommand();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	public void Off(int channel)
	{
		ElectrictCommand controlSH=new ElectrictCommand(System, COMMAND_TYPE.ON_OFF, SubnetID, DeviceID, channel, 0, 0);
		try {
			controlSH.sendCommand();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	public void Reverse(int channel)
	{

		ElectrictCommand controlSH=new ElectrictCommand(System, COMMAND_TYPE.REVERSE, SubnetID, DeviceID, channel, 0, 0);
		try {
			controlSH.sendCommand();
		} catch (Exception e) {
			// TODO: handle exception
		}


	}
//
//	public void readStatusUDP()
//	{
//
//		ElectrictCommand controlSH=new ElectrictCommand(System.CurrentAddress,System.ControlPort);
//		try {
//			controlSH.sendCommand(SYSTEM_TYPE.ARINA,COMMAND_TYPE.READ_STATUS, SubnetID, DeviceID, 1, 100);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//
//
//	}
	public boolean[] readStatusHTTP()
	{

		//String header="Send:020/100/0033/ HTTP/1<br>Response:";
		String strURL="";
		switch (System.SytemType) {
		case SBUS:
			strURL = makeURLReadStatusHTTPSbus();
			break;
		case ARINA:
			strURL=makeURLReadStatusHTTPARINA();
			break;
		default:
			break;
		}


		try {

			HttpURLConnection IPConnect=(HttpURLConnection)new URL(strURL).openConnection();
			IPConnect.connect();
			InputStream response=IPConnect.getInputStream();
			String strResponse=readIt(response, 66);
			char[] tempResponse=new char[NumberChannel];
			strResponse.getChars(strResponse.length()-NumberChannel, strResponse.length(), tempResponse, 0);
			Status=charArray2BooleanArray(tempResponse);

			return Status;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}
	private String makeURLReadStatusHTTPSbus() {
		String strSubnetID=convertTo3ByteString(SubnetID);
		String strDeviceID = convertTo3ByteString(DeviceID);
		String strURL="http://"+System.CurrentAddress+":"+Integer.toString(System.ControlPort)+"/smarttnh/123456/"+strSubnetID+"/"+strDeviceID+"/0033/";
		return strURL;
	}

	private String makeURLReadStatusHTTPARINA() {
		String strSubnetID=convertTo3ByteString(SubnetID);
		String strDeviceID = convertTo3ByteString(DeviceID);
		String strURL="http://"+System.CurrentAddress+":"+Integer.toString(System.ControlPort)+"/SMARTHOUSE/"+"/smarttnh/123456/0033/"+strSubnetID+"/"+strDeviceID;
		return strURL;
	}
	private static boolean[] charArray2BooleanArray(char[] chars)
	{
		boolean[] bits = new boolean[chars.length];
		for(int i=0;i<chars.length;i++)
		{
			if(chars[i]=='1')
				bits[i]=true;
			else
				bits[i]=false;

		}
		return bits;
	}
	private String convertTo3ByteString(int toConvert) {
		String strDeviceID="250";
		if(toConvert<10)
			strDeviceID="00"+ Integer.toString(toConvert);
		else if(toConvert<100)
			strDeviceID="0"+ Integer.toString(toConvert);
		else if(toConvert<1000)
			strDeviceID=Integer.toString(toConvert);
		return strDeviceID;
	}

	private String readIt(InputStream stream, int len)  {
		Reader reader = null;
		char[] buffer = new char[len];

		try {
			reader = new InputStreamReader(stream, "UTF-8");
			try {
				reader.read(buffer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new String(buffer).trim();
	}


}
